package com.woniu.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
public class AppConfig {

    public static final String QUEUE_NAME = "queue.name";
    public static final String QUEUE_NAME2 = "subscribe.queue";
    public static final String EXCHANGE_NAME = "outpatient.exchange";
    public static final String RK_NAME = "rk.name";
    public static final String RK_NAME2 = "subscribe.rk";

    @Bean
    public Queue queue() {
        //过期转发给死信交换机
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", "dlExchange");
        map.put("x-dead-letter-routing-key", "dl.key");
        return new Queue(QUEUE_NAME, false, false, false, map);
    }

    //预约队列
    @Bean
    public Queue subscribeQueue() {
        return new Queue(QUEUE_NAME2, false, false, false);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(topicExchange()).with(RK_NAME);
    }
    //预约绑定
    @Bean
    public Binding subscribeBinding() {
        return BindingBuilder.bind(subscribeQueue()).to(topicExchange()).with(RK_NAME2);
    }

    //死信队列
    @Bean
    public Queue dlQueue() {
        return new Queue("dlQueue", false, false, false);
    }

    //死信交换机
    @Bean
    public TopicExchange dlExchange() {
        return new TopicExchange("dlExchange");
    }

    //绑定死信队列和交换机
    @Bean
    public Binding dlBinding() {
        return BindingBuilder.bind(dlQueue()).to(dlExchange()).with("dl.key");
    }

    //发送对象，json转换
    @Bean
    public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer, CachingConnectionFactory connectionFactory) {

        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        configurer.configure(rabbitTemplate, connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.setConfirmCallback((CorrelationData correlationData, boolean b, String s) -> {

            log.info("接收到的参数为：{}，是否正常：{}，原因：{}", correlationData, b, s);

            if(b) {
                log.info("mq接收成功");
            }else {
                log.info("mq接收失败");
            }
        });

        return rabbitTemplate;
    }
}
